<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" label-width="120px" :rules="rules" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 用  户  名 :"   :required="true"  prop="admin_name" >
              <el-input v-model="postForm.admin_name" disabled :maxlength="30"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 密  码 : "   :required="true"  prop="password" >
              <el-input  v-model="postForm.password" type="password" :maxlength="20"   />
            </el-form-item>
          </el-col>
        </el-row>


        <el-col :span="12" style="padding-left:60px;">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
          <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
        </el-col>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { admindelete , adminUpdate ,adminDetail} from '@/api/admin'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'

const defaultForm = {
  id :'',
  //admin_name: '', //名称
  password: '', // 编码
  //site_id: 0, //联系人
  //role_id: 0, // 描述
  email: '', // 用户手机号码
  is_del:0
}

export default {
  name: 'AdminEdit',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {

        if(rule.field == 'password'){
          callback(new Error('密码为必传项！'))
        }
      } else {
        callback()
      }
    }

    return {
      is_del: 0 ,
      postForm: Object.assign({}, defaultForm),
      editForm:{
        id :'',
        //admin_name: '',
        password: '',
        email: '',
        //site_id: 0,
        //role_id: 0
        is_del: 0
      },
      loading: false,
      userListOptions: [],
      title:'',
      rules: {
        //admin_name: [{ validator: validateRequire }],
        password: [{ validator: validateRequire }]
      }
    }
  },
  computed: {

  },
  created() {
    if (this.isEdit) {
      const id = this.$route.query && this.$route.query.id
      this.adminDetail( id )
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    adminDetail(id) {
      adminDetail({id:id}).then(response => {
        this.postForm = response.data.data
        this.title += '  商家 Id:${this.postForm.id}'
        console.log(this.postForm)
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    doSubmitForm() {
      this.editForm.id = this.postForm.id
      this.editForm.password = this.postForm.password
     // this.editForm.admin_name = this.postForm.admin_Name
      this.editForm.email = this.postForm.email

      adminUpdate(this.editForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
          this.$refs['postForm'].resetFields()
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
